/**
 * Created by TianJing on 2017/3/21.
 */
window.Husky=function (form) {
    var self=this;
    this.form=$(form);
    this.radio=self.form.find(".husky-radio");
    this.checkBoxes=self.form.find(".husky-checkbox");

};
Husky.prototype={
    createRadio:function (options) {
        var self=this;
        var parentBox=$(options.parent);
        var configJson=$(options.configJson);
        var hasIcon=options.hasIcon||false;
        var callback=options.callback;
        var domList=[];
        !!hasIcon?$.each(configJson,function (idx, ele) {
            domList.push('<li data-value="'+ele.value+'"><i></i><span>'+ele.text+'</span></li>')
        }):$.each(configJson,function (idx, ele) {
            domList.push('<li data-value="'+ele.value+'">'+ele.text+'</li>')
        });
        parentBox.html(domList.join(""));
        self.radio.off("click").on("click","li",function (e) {
            var $this=$(this);
            var selfIndex=$this.index();
            $this.hasClass("chosen")?$this.removeClass("chosen").parent().attr("data-value",""):$this.addClass("chosen").siblings().removeClass("chosen").parent().attr("data-value",configJson[selfIndex]['value']);
            !!callback && $.isFunction(callback) && callback(selfIndex);
            e.stopPropagation();
        });

    },
    getJsonByForm:function () {
        var self=this;
        var json={};
        self.radio.length?$.each(self.radio, function (idx, ele) {
            var itm=$(ele);
            itm.attr("name")?json[itm.attr("name")]=itm.attr("data-value")||"":null;
        }):null;
        // self.checkBoxes.length?$.each(self.checkBoxes, function (idx, ele) {
        //     var itm=$(ele);
        //     itm.attr("name")?json[itm.attr("name")]=itm.attr("data-value")||"":null;
        // }):null;
        return json;
    }

};